<template>
  <div style="padding: 30px;width: 400px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="药品名称" prop="drug_name">
        <el-input v-model="ruleForm.drug_name"></el-input>
      </el-form-item>
      <el-form-item label="药品图片" prop="drug_img">
        <el-upload
            class="avatar-uploader"
            action=""
            :show-file-list="false"
            :http-request="upload"
            :before-upload="beforeAvatarUpload">
          <img v-if="ruleForm.drug_img" :src="ruleForm.drug_img" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="药品信息" prop="drug_info">
        <el-input type="textarea" v-model="ruleForm.drug_info"></el-input>
      </el-form-item>
      <el-form-item label="药品疗效" prop="drug_effect">
        <el-input type="textarea" v-model="ruleForm.drug_effect"></el-input>
      </el-form-item>
      <el-form-item label="销售地点" prop="saleIds">
        <el-select v-model="ruleForm.saleIds" multiple placeholder="请选择销售地点" style="width: 100%;">
          <el-option v-for="item in sales" :key="item.sale_id" :label="item.sale_name" :value="item.sale_id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import {saleList,uploadImg,addDrug} from "@/api/drug";

export default {
  data () {
    return {
      ruleForm:{
        drug_name:'',
        drug_info:'',
        drug_effect:'',
        saleIds:'',
        drug_img:''
      },
      sales:[], //加载下拉框选项
      rules: {
        drug_name: [
          { required: true, message: '请输入药品名称', trigger: 'blur' },
        ],
        drug_info: [
          { required: true, message: '请输入药品信息', trigger: 'blur' }
        ],
        drug_effect: [
          { required: true, message: '请输入药品疗效', trigger: 'blur' }
        ],
        saleIds: [
          { required: true, message: '请选择销售地点', trigger: 'change' }
        ],
        drug_img: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ]
      }
    };
  },
  components: {

  },
  created() {

  },
  mounted() {
    this.initSelectOption()
  },
  methods: {
    initSelectOption(){
      saleList().then(res=>{
        this.sales=res.data
      }).catch(error=>{})
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    upload(param){
      //上传药品图片
      const formData=new FormData()

      formData.append("file",param.file)

      uploadImg(formData).then(res=>{

        this.$message({
          type:"success",
          message:res.msg
        })

        this.ruleForm.drug_img=res.data

      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //发送添加请求
          addDrug(this.ruleForm).then(res=>{
            if(res.code==200){
              //添加成功
              this.$message({
                type:"success",
                message:res.msg
              })
              //关闭窗口
              this.$emit("closeAddDialog")
            }else if(res.code==101){
              //token问题，需要重新登录
              this.$message({
                type:"error",
                message:res.msg
              })
              this.$router.push("/login")
            }else{
              this.$message({
                type:"error",
                message:res.msg
              })
            }


          }).catch(error=>{})

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
